<template>
  <my-simple-page
    :tableTitle="tableTitle"
    :search-fields="searchFields"
    :columns="columns"
    :edit-fields="editFields"
    :search-url="searchUrl"
    :save-url="saveUrl"
    :delete-url="deleteUrl">
  </my-simple-page>
</template>

<script>
  export default {
    name: "user-index",
    data(){
      return{
        searchFields: [
          {
            type: 'input',
            label: '姓名',
            prop: 'name'
          },{
            type: 'select',
            label: '状态',
            prop: 'status',
            dict: 'COMMON_STATUS_DICT',
          },{
            type: 'date',
            label: '生日',
            prop: 'birthday'
          }
        ],
        tableTitle: '用户列表',
        columns: [
          {label:'账号',prop: 'loginName'},
          {label:'姓名',prop: 'name'},
          {label:'角色',prop: 'role',render: this.renderRoles},
          {label:'邮箱',prop: 'email'},
          {label:'电话',prop: 'phone'},
          {label:'生日',prop: 'birthday'},
          {label:'状态',prop: 'status',render: this.renderStatus},
          {label:'备注',prop: 'userDesc'}],
        editFields: [
          {
            type: 'input',
            label: '姓名',
            prop: 'name',
            rules:{ required: true, message: '姓名不能为空', trigger: 'blur' }
          },
          {
            type: 'input',
            label: '账号',
            prop: 'loginName',
            rules:{ required: true, message: '账号不能为空', trigger: 'blur' }
          },
          {
            type: 'input',
            label: '邮箱',
            prop: 'email'
          },
          {
            type: 'input',
            label: '电话',
            prop: 'phone'
          },
          {
            type: 'date',
            label: '生日',
            prop: 'birthday'
          },
          {
            type: 'select',
            label: '角色',
            prop: 'role',
            url: '/api/sys-config/role/getRoleOption'
          },
          {
            type: 'switch',
            label: '状态',
            prop: 'status',
            dict: 'COMMON_STATUS_DICT'
          },
          {
            type: 'textarea',
            label: '备注',
            prop: 'userDesc'
          }
        ],
        searchUrl: '/api/sys-config/user/getUserList',
        saveUrl: '/api/sys-config/user/saveUser',
        deleteUrl: '/api/sys-config/user/deleteUser',
        roleDict: [],
        statusDict: []
      }
    },
    methods:{
      renderStatus(val){
        for(let item of this.statusDict){
          if (item.value == val){
            return item.label;
          }
        }
      },
      renderRoles(val){
        for(let item of this.roleDict){
          if (item.value == val){
            return item.label;
          }
        }
      },
    },
    created(){
      this.$ajax.getDict('COMMON_STATUS_DICT').then(res=>{
        this.statusDict = res;
      })
      this.$ajax.getAllRole().then(res=>{
        this.roleDict = res;
      })
    }
  }
</script>

<style scoped>

</style>
